package com.icloud.common.web.exception;

import cn.hutool.core.util.ArrayUtil;
import com.icloud.common.core.api.CommonResult;
import com.icloud.common.web.util.I18nUtil;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;
import java.util.Arrays;

/**
 * 全局异常处理
 * Created by im on 2020/2/27.
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public CommonResult handle(ApiException e) {
        String messageResult = I18nUtil.getMessageResult(e.getMessage());
        if(e.getArguments() != null && e.getArguments().length > 0) {
            messageResult = MessageFormat.format(messageResult, e.getArguments());
        }
        if (e.getErrorCode() != null) {
            return CommonResult.failed(e.getErrorCode(), messageResult);
        }
        return CommonResult.failed(messageResult);
    }

    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public CommonResult handleValidException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                //验证错误信息兼容国际化和占位符
                //获取参数
                Object[] arguments = fieldError.getArguments();
                if(arguments != null && arguments.length > 0) {
                    //移除数组第一个元素
                    Object[] finalArguments = arguments;
                    arguments = Arrays.stream(finalArguments, 1, arguments.length).toArray();
                    //数组反转
                    arguments = ArrayUtil.reverse(arguments);
                }
                String field = fieldError.getField();
                String messageResult = I18nUtil.getMessageResult(fieldError.getDefaultMessage());
                message = field + " " + MessageFormat.format(messageResult, arguments);
            }
        }
        return CommonResult.validateFailed(message);
    }

    @ResponseBody
    @ExceptionHandler(value = BindException.class)
    public CommonResult handleValidException(BindException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                //验证错误信息兼容国际化
                //获取参数
                Object[] arguments = fieldError.getArguments();
                if(arguments != null && arguments.length > 0) {
                    //移除数组第一个元素
                    Object[] finalArguments = arguments;
                    arguments = Arrays.stream(finalArguments, 1, arguments.length).toArray();
                    //数组反转
                    arguments = ArrayUtil.reverse(arguments);
                }
                String field = fieldError.getField();
                String messageResult = I18nUtil.getMessageResult(fieldError.getDefaultMessage());
                message = field + " " + MessageFormat.format(messageResult, arguments);
            }
        }
        return CommonResult.validateFailed(message);
    }
}
